package com.clothing.customization.controller;

import com.clothing.customization.common.ApiResponse;
import com.clothing.customization.common.PageResult;
import com.clothing.customization.entity.ClothingStyle;
import com.clothing.customization.service.ClothingStyleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/clothing/style")
@Tag(name = "服装款式管理", description = "服装款式增删改查接口")
public class ClothingStyleController {

    @Autowired
    private ClothingStyleService clothingStyleService;

    @GetMapping("/list")
    @Operation(summary = "获取款式列表", description = "分页获取款式列表，支持按名称和状态筛选")
    public ApiResponse<PageResult<ClothingStyle>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "款式名称") @RequestParam(required = false) String name,
            @Parameter(description = "分类ID") @RequestParam(required = false) Integer categoryId,
            @Parameter(description = "状态(1-启用 0-禁用)") @RequestParam(required = false) Integer status
    ) {
        PageResult<ClothingStyle> result = clothingStyleService.getStyleList(pageNum, pageSize, name, categoryId, status);
        return ApiResponse.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取款式详情", description = "根据ID获取款式详情")
    public ApiResponse<ClothingStyle> getById(@PathVariable Long id) {
        ClothingStyle style = clothingStyleService.getById(id);
        return ApiResponse.success(style);
    }

    @PostMapping
    @Operation(summary = "添加款式", description = "添加新的服装款式")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<ClothingStyle> add(@Valid @RequestBody ClothingStyle style) {
        ClothingStyle result = clothingStyleService.addStyle(style);
        return ApiResponse.success(result);
    }

    @PutMapping
    @Operation(summary = "更新款式", description = "更新已有的服装款式")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<ClothingStyle> update(@Valid @RequestBody ClothingStyle style) {
        ClothingStyle result = clothingStyleService.updateStyle(style);
        return ApiResponse.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除款式", description = "根据ID删除服装款式")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<Boolean> delete(@PathVariable Long id) {
        boolean result = clothingStyleService.deleteStyle(id);
        return ApiResponse.success(result);
    }

    @PutMapping("/status")
    @Operation(summary = "更新款式状态", description = "启用或禁用款式")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ApiResponse<Boolean> updateStatus(
            @Parameter(description = "款式ID") @RequestParam Long id,
            @Parameter(description = "状态(1-启用 0-禁用)") @RequestParam Integer status
    ) {
        boolean result = clothingStyleService.updateStatus(id, status);
        return ApiResponse.success(result);
    }
} 